﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Common;
using Twinkle.Tweak;
using Krile.Kernel.WindowsEx;

namespace Twinkle.Dialogs.Account
{
    public partial class ListInformationUpdate : Form
    {
        Data.Account target;
        public ListInformationUpdate(Data.Account a)
            : this()
        {
            target = a;
        }

        public ListInformationUpdate()
        {
            InitializeComponent();
        }

        private void SetProgressMax(int val)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int>(SetProgressMax), val);
            }
            else
            {
                progress.Maximum = val + 1;
                progress.Value = 1;
                progress.Style = ProgressBarStyle.Continuous;
            }
        }

        private void Progress()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(Progress));
            }
            else
            {
                progress.Value++;
                progress.Refresh();
            }
        }

        private void UpdateText(string text)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(UpdateText), text);
            }
            else
            {
                this.stateLabel.Text = text;
                stateLabel.Refresh();
            }
        }

        private void ListInformationUpdate_Shown(object sender, EventArgs e)
        {
            var action = new Action(UpdateListInformation);
            action.BeginInvoke((iar) => ((Action)iar.AsyncState).EndInvoke(iar), action);
        }

        private void UpdateListInformation()
        {
            if (target.RateLimitRemain < 2)
            {
                MessageBoxEx.Show(
                    "List update error",
                    "API limitation is too short to receive list information.",
                    "List information refreshing error", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
                this.Invoke(new Action(this.Close));
                return;
            }

            var nc = new NetConnect();
            UpdateText("Receiving following lists information...");
            List<TList> lists = new List<TList>();
            var tlists = target.CredentialProvider.GetFollowingListsAll(target.UserId);
            if (tlists == null)
            {
                UpdateText("List information is nothing.");
            }
            else
            {
                lists.AddRange(tlists);
                int lCount = lists.Count;
                SetProgressMax(lCount);
                if (target.RateLimitRemain < lCount)
                {
                    MessageBoxEx.Show(
                        "List update error",
                        "API limitation is too short to receive list information.",
                        "List information refreshing error", MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
                    this.Invoke(new Action(this.Close));
                    return;
                }
                foreach (var l in lists)
                {
                    UpdateText("Receiving information of list \"@" + l.ParentUserId + "/" + l.Id + "\"");
                    l.SetUsers(target.CredentialProvider.GetListMembersAll(l.ParentUserId, l.Id));
                    Progress();
                }
                UpdateText("Completed!");
                foreach (var l in lists)
                {
                    System.Diagnostics.Debug.WriteLine(l.Id + ":");
                    if (l.Members == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Lost information...");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(String.Join(Environment.NewLine, l.Members));
                    }
                }
                target.FollowingLists = lists.ToArray();
            }
            this.Invoke(new Action(this.Close));
        }
    }
}
